/**
 * com.monte.service.BaseServiceSupport.java
 * create by FengMy from 2013
 */
package com.monte.service;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.monte.entity.CoreEntity;
import com.monte.persistence.PersistenceManager;
import com.monte.persistence.QueryExecutor;

/**
 * @描述: 业务基类基础实现
 * @author FengMy
 * @since 2013-3-6
 */
public abstract class BaseServiceSupport<T extends CoreEntity> implements BaseService<T> {
	@Autowired
	protected PersistenceManager persistenceManager;
	@Autowired
	protected QueryExecutor queryExecutor;
	@Override
	public T addEntity(T entity) {
		return persistenceManager.addEntity(entity);
	}

	@Override
	public int addBatch(List<T> entities) {
		return persistenceManager.addBatch(entities);
	}

	@Override
	public T updateEntity(T entity) {
		return persistenceManager.updateEntity(entity);
	}

	@Override
	public int updateBatch(List<T> entities) {
		return persistenceManager.updateBatch(entities);
	}

	@Override
	public int deleteEntity(T entity) {
		return persistenceManager.deleteEntity(entity);
	}

	@Override
	public int deleteBatch(List<T> entities) {
		return persistenceManager.deleteBatch(entities);
	}

	@Override
	public T getEntityById(Class<T> type, Object id) {
		return persistenceManager.getEntityById(type,id);
	}
	
	@Override
	public T getEntityById(Object id) {
		Type[] types = ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments();
		if(types!=null && types.length > 0){
			@SuppressWarnings("unchecked")
			Class<T> c = (Class<T>)(types[0]);
			return getEntityById(c, id);
		}
		return null;
	}

}
